package com.nut.course.api;

import com.nut.course.common.Result;
import com.nut.course.entity.OssFile;
import com.nut.course.service.SystemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/system")
public class SystemController {

    @Autowired
    private SystemService systemService;

    @PostMapping("/uploadImg")
    public Result<OssFile> uploadImg(@RequestParam("file") MultipartFile multipartFile) {
        return systemService.uploadImg(multipartFile);
    }

    @GetMapping("/help")
    public void help(HttpServletResponse response) {

        StringBuilder sb = new StringBuilder();
        sb.append("\r\n getCacheSize");
        sb.append("\r\n removeCache?key= ");
        sb.append("\r\n clean");
        sb.append("\r\n sessions  查看xms session");
        String str = sb.toString();

        outputHtml(response, str);
    }

    private void outputHtml(HttpServletResponse response, String str) {
        response.setContentType("text/plain;charset=UTF-8");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        try {
            response.getWriter().write(str);
            response.getWriter().flush();
            response.getWriter().close();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }


}
